package com.malou.client.ventas.cajainicial;

import java.util.Date;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;

public class CajaInicialForm extends FormPanel {
	
	private NumberField saldoinicial;
	//private DateField fechaactual;
	private Button guardar;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public CajaInicialForm(){
		setHeading("Caja Inicial");
		setSize(280, 130);
		setFrame(true);
		setLabelAlign(LabelAlign.TOP);
		setButtonAlign(HorizontalAlignment.CENTER);
		   
	    mask(constants.cargando());
	    
	    guardar = new Button(constants.guardar());
	    guardar.hide();
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				if(validate()){
					CajaInicial balance = new CajaInicial();
					//String fechaactual = DateTimeFormat.getFormat("yyyy-MM-dd").format(new Date());
					balance.setFecha(new Date());
					Double monto = Double.valueOf(saldoinicial.getValue().toString());
					balance.setSaldoinicial(monto);					
					CajaIniciales.save(balance);
					mask(constants.guardando());
				}
			}
	    	
	    });
	    addButton(guardar);
	    /*fechaactual = new DateField();	 
	    fechaactual.hide();
	    fechaactual.setFieldLabel("fecha");	
	    fechaactual.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));	    
	    add(fechaactual, new FormData("-20"));
	    fechaactual.setValue(new Date());	    
	    */
	    //String fechaactual = DateTimeFormat.getFormat("yyyy-MM-dd").format(new Date());
	    CajaIniciales.cif = this;
	    CajaIniciales.checkSaldoInicial(new Date());
	    //Date t = DateTimeFormat.getFormat("yyyy-MM-dd").parse("2010-12-01");
	    
	    //String f[] =fechaactual.split("-");
	    //System.out.println(t);
	    		
	}
	
	public void addMessage(){
		guardar.hide();
		if(saldoinicial != null)
		saldoinicial.hide();
		unmask();
		add(new Label(constants.montoInicialIngresado()));
		layout();		
		
	}
	
	public void addFields(){
		unmask();
		saldoinicial = new NumberField();		
		saldoinicial.setFieldLabel(constants.saldoInicial());	    
		saldoinicial.setAllowBlank(false);		
	    add(saldoinicial, new FormData("-20"));
	    guardar.show();
	    
	    layout();    
	    
	}
	
	protected boolean validate() {
		return saldoinicial.isValid();
	}
}
